/**
 * Created by zxx on 2015-11-16.
 */

/**
 *
 * ConfigDataGrid
 *      属性：
 *          - defineAction 列和查询定义Action
 *          - showCondition 是否显示查询条件，默认为true
 *          - showoperation 是否显示查看 true为显示
 */
function ConfigDataGrid() {}

ConfigDataGrid.inherit(DataGrid);

/**
 * 初始化方法
 */
ConfigDataGrid.prototype.init = function () {
    ConfigDataGrid.base.init.call(this);
}

ConfigDataGrid.prototype.create = function() {
    ConfigDataGrid.base.create.call(this);
    this.initDefine();
}

ConfigDataGrid.prototype.initDefine = function() {
    var soap = new HWSOAP(this.getAttribute().defineaction);
    soap.send();
    var oResult = soap.getResult();

    this.initColumn(oResult.column_format, null, this.getAttribute().showoperation);
    if (this.getAttribute().showcondition &&
    	this.getAttribute().showcondition == "false") {
    	return;
    }
    this.initParam(oResult.query_param);
}

/**
 * 初始化列
 * @param oData
 */
ConfigDataGrid.prototype.initColumn = function(oData, sParentPath, showoperation) {
    for (var i = 0 ; i < oData.length ; i++) {
        var oColData = oData[i];

        var path = oColData["@nm"];
        if (sParentPath) path = sParentPath + "." + path;

        if (oColData.col) {
            this.initColumn(oColData.col, path);
        }
        if (!oColData["@pwd"]) continue;
        var sColNode = "<div data-control='Col' path='" + path + "' title='" + oColData["@tt"] + "' " +
            " width='" + oColData["@pwd"] + "' sortable='true' align='center' sort='person_"+ path +"' />";
        if (!this.getElement()) {
            this.setElement($("<div/>")[0]);
        }
        $(this.getElement()).append(sColNode);
    }
    if(showoperation) {
	    var eColNode = "<div data-control='Col' title='操作' formatter='button' text='查看' width='100' sortable='false' align='center' click='{openDetails}'/>";
	    $(this.getElement()).append(eColNode);
    }
}

/**
 * 初始化查询条件
 * @param oData
 */
ConfigDataGrid.prototype.initParam = function(oData) {
    //初始化查询面板
    var oPanel = Control.newInstance("Panel", {datasource:"", "class":"form-horizontal pop-query"});
    oPanel.init();
    $(this.getHTML()).prepend(oPanel.getHTML());
    oPanel.initLoad();
    this._queryPanel = oPanel;
    for (var i = 0 ; i < oData.length ; i++) {
        var oParamData = oData[i];
        if (i % 2 == 0) {
            var oRow = oPanel.addItem("Group", {"class":"row"});
        }
        if(typeof(oParamData.hidden)!="undefined"&&oParamData.hidden=="true"){//支持默认隐藏
            oRow.addItem("Label", {text:oParamData.title+":", "class":"col-xs-2 control-label-right","style":"display:none"});
        }else{
            oRow.addItem("Label", {text:oParamData.title+":", "class":"col-xs-2 control-label-right"});
        }
        oParamData.path = oParamData.name;
        if (oParamData["class"]) {
            oParamData["class"] = oParamData["class"] + " col-xs-4";
        } else {
            oParamData["class"] = "col-xs-4";
        }
        oParamData.title = "";
        var oInput = oRow.addItem(oParamData["data-control"], oParamData);
        $(".input-group-addon", oInput.getHTML()).css("lineHeight", "36px");
        $(oInput.getHTML()).removeClass("control-input"); //解决表单样式问题.
    }
    //增加按钮
    if (i % 2 == 0) {
        var oRow = oPanel.addItem("Group", {"class":"row"});
        oRow.addItem("Group",{"class":"col-xs-10"});
    } else {
        oRow.addItem("Group",{"class":"col-xs-4"});
    }
    var oQueryBtn = oRow.addItem("Button", {text:"查询", "class":"btn-primary"});
    var oClearBtn = oRow.addItem("Button", {text:"清空"});
    //重复绑定一次数据，目的就是触发动态控件的initdata事件
    var oParam = this._queryPanel.getData();
    this._queryPanel.setValue(oParam);
    var that = this;
    $(oQueryBtn.getHTML()).bind("click", function(e) {
        var oParam = that._queryPanel.getData();
        that._queryPanel.setValue(oParam);
        that.queryAndDisplay(oParam);
    });
    $(oClearBtn.getHTML()).bind("click", function(e) {
        that._queryPanel.clearForm();
    });
}

ConfigDataGrid.prototype.reload = function(bAsync) {
	this._queryPanel.clearForm();
	this.queryAndDisplay({}, bAsync);
}
